<?php
	
function list_pos_biaya(){

//PASANG JAVASCRIPT
	my_set_code_js('

	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com=pos_biaya&task=delete&id=\'+id;
		}
		return false;
	}

	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Kode'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Deskripsi'=>array('style'=>'border-bottom:2px solid;width:75%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

		$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	
	$query = "SELECT * FROM pos_biaya";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 35; 
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=pos_biaya&task=editpos&id='.$ey['pos_biaya_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['pos_biaya_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'kode'=> position_text_align ( $ey['code'] , 'center'),   
			'label'=>  $ey['label'] ,     
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	$paging = $kgPagerOBJ ->showPaging();
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pos_biaya&task=editpos&id=0\' })'),
		//'Export to excel'=>array('onclick'=>'javascript:location.href=\'index.php?com=csv&task=excel_daftar_karyawan\''),
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=all_karyawan\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	); 
	$printmenu = additional_menu_on_list( $menuontop ,'');
	return   table_builder($header , $datas ,  5 , false,$paging   );
}


function form_pos_biaya_submit($id){
	$datas = array(  
		'code'=>my_type_data_str( $_POST['kode'] ),
		'label'=>my_type_data_str( $_POST['label'] ), 
	);
	if($id > 0){
		return my_update_record('pos_biaya' ,'pos_biaya_id' , $id , $datas);
	}
	
	return my_insert_record('pos_biaya' , $datas);
}

function form_pos_biaya_create($id = 0){
	$view = form_header( "pos_biaya" , "pos_biaya"  );
 	$fields = my_get_data_by_id('pos_biaya','pos_biaya_id',$id); 
	
	$kode = array(
		'name'=>'kode',
		'value'=>( isset($_POST['kode']) ? $_POST['kode'] : $fields['code'] ),
		'id'=>'kode',
		'type'=>'text',
		'size'=>'5'
	);
	$form_field_kode = form_dynamic($kode);
	
	$view .= form_field_display( $form_field_kode   , "Kode"   );
	$label = array(
		'name'=>'label',
		'value'=>( isset($_POST['label']) ? $_POST['label'] : $fields['label'] ),
		'id'=>'label',
		'type'=>'text',
		'size'=>'45'
	);
	$form_field_nama = form_dynamic($label); 
	$view .= form_field_display( $form_field_nama   , "Deskripsi"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_pos_biaya_validate($id){
	$errsubmit = false;
	$err = array();
	
	$code =  trim($_POST["kode"]); 	 
	$query="SELECT pos_biaya_id FROM pos_biaya 
		WHERE code='".$code.
		"' AND pos_biaya_id<>".$id; 

	if( $code == ''){
		$errsubmit = true;
		$err[] = "Kode Cost Centre belum di isi";
	}

	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode sudah digunakan";	
	}	
    
	/*CHECK LABEL BIAYA*/
	$label = trim($_POST["label"]);
	if($label == ''){
		$errsubmit = true;
		$err[] = "Deskripsi biaya belum diisi";
	}
	 
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function remove_pos_biaya_id($id){
	$query= "DELETE FROM pos_biaya WHERE pos_biaya_id = {$id}";
	return my_query($query);
}  
 